/**
 * Created by Admin on 2018/1/4.
 */
define(function (require, exports, module) {
    var template = require("template");
    var loadingTpl = require("../common/tpl/loading.html");
    var tpl = require("./tpl/patTpl.html");
    require("../common/highchart/highcharts");
    require("../common/highchart/columnChart");
    require("../common/highchart/piechart");
    require("../common/qy-template");
    var pat = {
        initTable: function (apiUrl, exportUrl, keyName, sourceData, selfTitle) {
            $("#validptchart-tab").hide();
            $(".sta-year-chart").html(template.compile(loadingTpl));
            $("#ptamountchart").html(template.compile(loadingTpl));
            $("body").on("click", ".down", function () {
                $(this).removeClass("down").addClass("up");
                $("[name=order]").val("asc");
                $("[name=sort]").val($(this).attr("data-sort"))
            }).on("click", ".up", function () {
                $(this).removeClass("up").addClass("down");
                $("[name=order]").val("desc");
                $("[name=sort]").val($(this).attr("data-sort"))
            })
            $.ajax({
                url: window.jkconfig.webapiurlv + apiUrl + "?" + sourceData,
                success: function (data) {
                    $(".la-square-jelly-box.la-2x").hide();
                    if (data.code == 0 && data.list.length > 1) {
                        $("#validptchart-tab").show();
                        $(".nodata-title").hide();
                        /*获得表格数据*/
                        var html = template.compile(tpl)({data: data, keyName: keyName})
                        $("#validptchart-tab").html(html);

                        /*遍历info,得到四个数组：Name=[],fmcount=[],syxxxount=[],wgsjcount=[]*/
                        for (var i = 0, Name = [], fmcount = [], syxxcount = [], wgsjcount = []; i < data.list.length; i++) {
                            Name.push(data.list[i][keyName]);
                            fmcount.push(data.list[i].fmcount);
                            syxxcount.push(data.list[i].syxxcount);
                            wgsjcount.push(data.list[i].wgsjcount);
                        }
                        var sourceData = [{name: '发明专利', data: fmcount},
                                {name: '实用新型', data: syxxcount},
                                {name: '外观专利', data: wgsjcount}
                            ],
                            //线形图初始化
                            linechart = new ColumnChart({
                                dom: "ptamountchart",
                                xData: Name,
                                yData: sourceData,
                                title: selfTitle,
                                legend: {
                                    layout: 'top',
                                    align: 'right',
                                    floating: false
                                }
                            });

                        /*subtitle数组*/
                        /*用于记录合计*/
                        var totalA = [];
                        $.each($(".totaleC"), function () {
                            totalA.push($(this).text());
                        });
                        if (typeof(totalA[0]) == "undefined") {
                            totalA[0] = 0;
                        }
                        piedata = [];

                        for (var i = 0; i < data.list.length; i++) {
                            var middleData = [];
                            middleData[0] = {name: '发明专利', y: data.list[i].fmcount};
                            middleData[1] = {name: '实用新型', y: data.list[i].syxxcount};
                            middleData[2] = {name: '外观专利', y: data.list[i].wgsjcount};
                            piedata.push(middleData);
                        }
                        var subtitle = "共" + totalA[0] + "件",
                            newPieChart = new PieChart({//饼图初始化
                                dom: "areaChart",
                                piedata: piedata,
                                pietitle: selfTitle,//饼图一级标题
                                subtitle: subtitle//饼图二级标题，
                            }),
                            valChartTab = $("#validptchart-tab tr");//饼状图改变容器
                        new PieChange(valChartTab, false, true, newPieChart, totalA, selfTitle, "全部");
                    } else {
                        // $("#validptchart-tab").html("暂无数据！");
                        $("#validptchart-tab").hide();
                        $("#ptamountchart").html("<div class='nodata-title' style='height:270px;line-height:270px;'>暂无数据</div>");
                        $(".nodata-title").show();
                        $(".sta-year-chart").hide();
                    }
                }
            });
            $("body").on("click", ".btnExport", function () {
                var sort = $("[name=sort]").val();
                var order = $("[name=order]").val();
                window.location.href = window.jkconfig.webapiurlv + exportUrl + "?sort=" + sort + "&order=" + order + "&" + api_key + "=" + token;
            })
        }
    }
    module.exports = pat;
})